package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.blankj.utilcode.util.ToastUtils;

import flc.ast.BaseAc;
import flc.ast.HomeActivity;
import flc.ast.R;
import flc.ast.databinding.ActivityInputPwdBinding;
import flc.ast.utils.Content;
import flc.ast.view.PasscodeView;
import stark.common.basic.utils.SPUtil;
import stark.common.basic.utils.StatusBarUtils;

public class InputPwdActivity extends BaseAc<ActivityInputPwdBinding> {

    public static boolean isShow = false;
    private String input = "";
    private Dialog myTipDialog;


    @Override
    protected int onCreate() {
        return R.layout.activity_input_pwd;
    }

    @Override
    protected boolean isStatusBarTextDark() {
        boolean isOpened = SPUtil.getBoolean(mContext, Content.IS_CALCULATOR_MODE, false);
        return !isOpened;
    }

    @Override
    protected void initView() {
        mDataBinding.passcodeView.setListener(new PasscodeView.PasscodeViewListener() {
            @Override
            public void onFail() {
                mDataBinding.passcodeView.getWrongStatusColor();
            }

            @Override
            public void onSuccess(String number) {
                startActivity(HomeActivity.class);
                finish();
            }
        });
        mDataBinding.passcodeView.setMyClickListener(new PasscodeView.MyClickListener() {
            @Override
            public void click() {
                String password = SPUtil.getString(mContext, Content.myPassword, "");
                ToastUtils.showShort(getString(R.string.pwd_tip, password));
            }
        });
        mDataBinding.ivJia.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                String password = SPUtil.getString(mContext, Content.myPassword, "");
                ToastUtils.showShort(getString(R.string.pwd_tip, password));
                return false;
            }
        });

        mDataBinding.ivCLear.setOnClickListener(this);
        mDataBinding.ivDelete.setOnClickListener(this);

        mDataBinding.ivNum0.setOnClickListener(this);
        mDataBinding.ivNum1.setOnClickListener(this);
        mDataBinding.ivNum2.setOnClickListener(this);
        mDataBinding.ivNum3.setOnClickListener(this);
        mDataBinding.ivNum4.setOnClickListener(this);
        mDataBinding.ivNum5.setOnClickListener(this);
        mDataBinding.ivNum6.setOnClickListener(this);
        mDataBinding.ivNum7.setOnClickListener(this);
        mDataBinding.ivNum8.setOnClickListener(this);
        mDataBinding.ivNum9.setOnClickListener(this);

        mDataBinding.ivNum0.setTag(0);
        mDataBinding.ivNum1.setTag(1);
        mDataBinding.ivNum2.setTag(2);
        mDataBinding.ivNum3.setTag(3);
        mDataBinding.ivNum4.setTag(4);
        mDataBinding.ivNum5.setTag(5);
        mDataBinding.ivNum6.setTag(6);
        mDataBinding.ivNum7.setTag(7);
        mDataBinding.ivNum8.setTag(8);
        mDataBinding.ivNum9.setTag(9);

        //提示弹框
        tipDialog();
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivNum0:
            case R.id.ivNum1:
            case R.id.ivNum2:
            case R.id.ivNum3:
            case R.id.ivNum4:
            case R.id.ivNum5:
            case R.id.ivNum6:
            case R.id.ivNum7:
            case R.id.ivNum8:
            case R.id.ivNum9:
                int number = (int) v.getTag();
                addChar(number);
                break;
            case R.id.ivCLear://清空
                clearChar();
                break;
            case R.id.ivDelete://删除
                deleteChar();
                break;
            //提示弹框
            case R.id.ivDialogTipConfirm:
                myTipDialog.dismiss();
                break;
        }
    }

    @Override
    protected void initData() {
        boolean isOpened = SPUtil.getBoolean(mContext, Content.IS_CALCULATOR_MODE, false);
        if (isOpened) {
            //计算器模式
            mDataBinding.llInputPwd.setVisibility(View.GONE);
            mDataBinding.llCalculator.setVisibility(View.VISIBLE);
        } else {
            mDataBinding.llInputPwd.setVisibility(View.VISIBLE);
            mDataBinding.llCalculator.setVisibility(View.GONE);
        }

        String password = SPUtil.getString(mContext, Content.myPassword, "");
        mDataBinding.passcodeView.setLocalPasscode(password);

        if (isShow){
            myTipDialog.show();
        }
    }

    private void addChar(int number) {
        input += number + "";
        mDataBinding.tvInput.setText(input);
        if (input.length() >= 4) {
            next();
        }
    }

    private void deleteChar() {
        if (input.length() == 0) return;
        input = input.substring(0, input.length() - 1);
        mDataBinding.tvInput.setText(input);
    }

    private void clearChar() {
        input = "";
        mDataBinding.tvInput.setText(input);
    }

    private void next() {
        String password = SPUtil.getString(mContext, Content.myPassword, "");
        if (input.equals(password)) {
            startActivity(HomeActivity.class);
            finish();
        } else {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    ToastUtils.showShort(getString(R.string.pwd_error));
                    clearChar();
                }
            }, 200);
        }
    }

    //提示弹框
    private void tipDialog() {
        myTipDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_tip, null);
        myTipDialog.setContentView(inflate);
        myTipDialog.setCancelable(true);
        Window dialogWindow = myTipDialog.getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.CENTER);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 1.0);
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        ImageView right = inflate.findViewById(R.id.ivDialogTipConfirm);
        right.setOnClickListener(this);
    }

}